<template>
    <div class="surface-preview">
        <template v-if="previewList.length > 0">
            <UxPopper v-for="preview in previewList" :key="preview.id" :custom="preview.custom_count > 0"
                      :alone="(preview.dtg.length + preview.emb.length) == 1">
                <button v-text="preview.group"></button>
                <template slot="pop">
                    <div class="surface-preview-dtg" v-if="preview.dtg.length > 0">
                        <h3>
                            <svg-icon name="design-dtg" />
                            <span v-text="preview.group"></span>
                        </h3>
                        <figure v-for="piece in preview.dtg" :key="piece.subname">
                            <span>
                                <img v-lazy="$getLitimgUrl(piece.output_img, 'small')" />
                                <b></b>
                            </span>
                            <small>
                                <svg-icon name="design-custom" v-if="piece.custom_count > 0" />
                                <em v-text="piece.name"></em>
                            </small>
                        </figure>
                    </div>

                    <div class="surface-preview-emb" v-if="preview.emb.length > 0">
                        <h3>
                            <svg-icon name="design-emb" />
                            <span v-text="preview.group"></span>
                        </h3>

                        <template v-for="piece in preview.emb">
                            <figure v-for="emb in piece.platens" :key="emb.id">
                                <span>
                                    <img v-lazy="$getImageUrl(emb.product_preview)" />
                                    <b></b>
                                </span>
                                <DesignerEmbPlatenName :name="emb.name" type="’’" />
                            </figure>
                        </template>

                    </div>

                </template>
            </UxPopper>
        </template>
        <template v-else>
            <button class="empty" v-text="'N/A'"></button>
        </template>
    </div>

</template>
<script>
export default {
    name: "DesignPrivew",
    props: {
        design: {
            type: Array,
            default: (i) => [],
        },
        placement: { type: String, default: 'right' }
    },
    data () {
        return {
            previewActive: null,
            target: null,
        };
    },
    computed: {
        // 预览位置
        previewList (item) {
            const design = this.design || [];
            const names = {

                // 衣服
                'Front Side - Middle': { group: 'Front', name: 'Center' },
                'Front Side - Top': { group: 'Front', name: 'Top' },
                'Front Side - Bottom': { group: 'Front', name: 'Bottom' },
                'Front Side - Lower Left': { group: 'Front', name: 'Bottom left' },
                'Front Side - Lower Right': { group: 'Front', name: 'Bottom right' },
                'Front Side - Shoulder Left': { group: 'Front', name: 'Left shoulder' },
                'Front Side - Shoulder Right': { group: 'Front', name: 'Right shoulder' },
                'Front Side - Pocket': { group: 'Front', name: 'Pocket' },
                'Front Side': { group: 'Front', name: 'Front' },


                'Back Side - Middle': { group: 'Back', name: 'Center' },
                'Back Side - Top': { group: 'Back', name: 'Top' },
                'Back Side - Bottom': { group: 'Back', name: 'Bottom' },
                'Back Side - Lower Left': { group: 'Back', name: 'Bottom left' },
                'Back Side - Lower Right': { group: 'Back', name: 'Bottom right' },
                'Back Side - Neck Label Outer': { group: 'Back', name: 'Outside neck label' },
                'Back Side': { group: 'Back', name: 'Emb' },


                'Sleeve Left': { group: 'Sleeve Left', name: 'Left sleeve' },
                'Sleeve Right': { group: 'Sleeve Right', name: 'Right sleeve' },
                'Long Sleeve Left': { group: 'Long Sleeve Left', name: 'Left long sleeve' },
                'Long Sleeve Right': { group: 'Long Sleeve Right', name: 'Right long sleeve' },
                'Neck Label Inner': { group: 'Neck Label Inner', name: 'Inside neck label' },

                // 长裤
                'Front Side - Left Leg Center': { group: 'Front', name: 'Center left leg' },
                'Front Side - Left Leg Top': { group: 'Front', name: 'Upper left leg' },
                'Front Side - Left Leg Bottom': { group: 'Front', name: 'Lower left leg' },
                'Front Side - Right Leg Center': { group: 'Front', name: 'Center right leg' },
                'Front Side - Right Leg Top': { group: 'Front', name: 'Upper right leg' },
                'Front Side - Right Leg Bottom': { group: 'Front', name: 'Lower right leg' },
                'Front Side - Left Leg': { group: 'Front', name: 'Left leg' },
                'Front Side - Right Leg': { group: 'Front', name: 'Right leg' },

                'Back Side - Left Leg Center': { group: 'Back', name: 'Center left leg' },
                'Back Side - Left Leg Top': { group: 'Back', name: 'Upper left leg' },
                'Back Side - Left Leg Bottom': { group: 'Back', name: 'Lower left leg' },
                'Back Side - Right Leg Center': { group: 'Back', name: 'Center right leg' },
                'Back Side - Right Leg Top': { group: 'Back', name: 'Upper right leg' },
                'Back Side - Right Leg Bottom': { group: 'Back', name: 'Lower right leg' },
                'Back Side - Left Pocket': { group: 'Back', name: 'Left pocket' },
                'Back Side - Right Pocket': { group: 'Back', name: 'Right pocket' },
                'Back Side - Left Leg': { group: 'Back', name: 'Left leg' },
                'Back Side - Right Leg': { group: 'Back', name: 'Right leg' },

                // 短裤
                'Front Side - Left Leg Pocket': { group: 'Front', name: 'Left Leg pocket' },
                'Front Side - Right Leg Pocket': { group: 'Front', name: 'Right Leg pocket' },
            }

            const surfaces = [];

            design.map((item) => {
                const info = names[item.canvas_name]
                const cache = info && info.group ? surfaces.find(i => i.group == info.group) : null;

                const piece = {
                    ...item,
                    type: item.type || 1,
                    platens: item.platens || item.platens_list || [],
                    name: (info || {}).name || this.$getSurfaceNameFormat(item.canvas_name),
                    group: (info || {}).group || this.$getSurfaceNameFormat(item.canvas_name),
                    custom_count: parseInt(item.custom_count) || 0,
                }

                try {
                    piece.layers = Array.isArray(piece.layers) ? piece.layers : JSON.parse(piece.layers || '[]');
                    piece.platens = Array.isArray(piece.platens) ? piece.platens : JSON.parse(piece.platens || '[]');
                } catch (err) {
                    console.log(err);
                }

                if (!piece.output_img) {
                    // 如果没有生成图片
                } else if (!info) {
                    // 没有对象
                    const surface = {
                        id: surfaces.length,
                        group: piece.group,
                        custom_count: piece.custom_count,
                        dtg: [],
                        emb: []
                    }

                    piece.type == 1 && surface.dtg.push(piece)
                    piece.type == 2 && surface.emb.push(piece)

                    surfaces.push(surface)

                } else if (!cache) {
                    // 没有缓存
                    const surface = {
                        id: surfaces.length,
                        group: piece.group,
                        custom_count: piece.custom_count,
                        dtg: [],
                        emb: []
                    }

                    piece.type == 1 && surface.dtg.push(piece)
                    piece.type == 2 && surface.emb.push(piece)

                    surfaces.push(surface)

                } else {
                    cache.custom_count += piece.custom_count
                    piece.type == 1 && cache.dtg.push(piece)
                    piece.type == 2 && cache.emb.push(piece)
                }
            });

            return surfaces;
        },
    },




};
</script>
<style lang="scss" scoped>
.surface-preview {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    color: #666666;
    line-height: 16px;

    &::v-deep .popper-content {
        padding: 4px;
        max-width: 682px;
        font-size: 0;
        cursor: default;
    }

    button {
        height: 26px;
        background: none;
        border: none;
        white-space: nowrap;
        cursor: default;
        height: 24px;
        background: rgba(0, 0, 0, 0.04);
        border-radius: 6px;
        padding: 4px;
        line-height: 16px;
        font-size: 14px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #000000;

        &:hover {
            color: #ea4c89;
        }

        &.empty {
            text-decoration: none;

            &:hover {
                color: #000000;
            }
        }
    }

    h3 {
        width: 100%;
        padding: 6px 0;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #212121;

        svg {
            width: 14px;
            height: 14px;
            margin-right: 4px;
        }

        span {
            text-align: center;
            font-size: 14px;
            font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            line-height: 16px;
        }
    }

    // surface-preview-dtg

    figure {
        display: inline-block;
        vertical-align: top;
        width: 160px;
        margin: 4px;
        position: relative;

        span {
            display: block;
            padding-bottom: 100%;
            position: relative;

            b {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                background: #f7f7f7;
                background-image: url('@/assets/images/loading.gif');
                background-size: 32px 32px;
                background-position: center center;
                background-repeat: no-repeat;
            }

            img {
                width: auto;
                height: auto;
                max-width: 100%;
                max-height: 100%;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);

                &[lazy='loaded'] {
                    background-image: url();

                    &+b {
                        display: none;
                    }
                }

                &[lazy='error']+b {
                    background-image: url('@/assets/images/error.png');
                    background-position: center center;
                    background-repeat: no-repeat;
                    background-size: 98px 98px;
                }
            }
        }

        span.emb-platen-name {
            padding-bottom: 0;
            background-color: transparent;
            text-align: center;
            font-size: 12px;
            line-height: 16px;
            margin-top: 6px;
            color: #212121;
        }

        small {
            display: flex;
            text-align: center;
            margin-top: 6px;
            font-size: 12px;
            font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            color: #212121;
            align-items: center;
            justify-content: center;
            line-height: 16px;

            svg {
                width: 14px;
                height: 14px;
                margin-right: 4px;

                &+em {
                    color: #3ea8cd;
                }
            }
        }
    }

    .popper+.popper {
        margin-top: 6px;
    }

    .popper[custom] {
        button {
            color: #3ea8cd;

            &:hover {
                color: #338cab;
            }
        }

        .surface-preview-dtg h3 {
            color: #3ea8cd;
        }
    }
}
</style>
